<?php
/**
 * Created by PhpStorm.
 * User: fengzi
 * Date: 2019/4/4
 * Time: 14:19
 */

namespace app\admin\logic;

use app\admin\logic\code\ErrorCode;
use app\common\model\Role;
use app\common\model\RoleOperation;
use think\Db;
use think\Error;

class RoleLogic extends BaseLogic {

    private $roleModel;

    public function __construct()
    {
        $this->roleModel = new Role();
    }

    /**
     * 角色列表
     * @param $data
     * @param $nowPage
     * @return mixed
     */
    public function roleList($tempWhere, $nowPage){
        if ($tempWhere['status'] && $tempWhere['status'] >= 0){
            $where[] = ['status', '=', $tempWhere['status']];
        }
        if ($tempWhere['name']){
            $where[] = ['name', 'LIKE', "%".$tempWhere['name']."%"];
        }
        $where[] = ['type', '=', 1];
        $count = $this->roleModel->where($where)->count();
        $list = $this->roleModel->where($where)->page($nowPage, self::$pageNum)->select();
        if ($count > 0){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $list, $count);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

    //详情
    public function info($id){
        $info = $this->roleModel->get($id);
        $operations = [];
        foreach ($info->operations as $key=>$val){
            $operations[] = $val->id;
        }
        $info['operation_arr'] = $operations;
        if ($info){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $info);
        } else{
            return ajaxArr(ErrorCode::$INFONULL, '信息不存在');
        }
    }

    //编辑
    public function handle($data){
        $act = $data['act'];
        $res = false;
        unset($data['act']);
        switch ($act){
            case 'add':
                //开启事务
                Db::startTrans();
                try{
                    $data['type'] = 1;
                    $roleRes = $this->roleModel->allowField(true)->save($data);
                    if (!$roleRes){
                        Db::rollback();
                        return ajaxArr(ErrorCode::$OPERATIONERROR, '角色录入失败');
                    }
                    foreach ($data['operation_id'] as $key=>$val){
                        $operationArr[] = $val;
                    }
                    $roleORes = $this->roleModel->operations()->saveAll($operationArr);
                    if (!$roleORes){
                        Db::rollback();
                        return ajaxArr(ErrorCode::$OPERATIONERROR, '角色权限录入失败');
                    }
                    $res = true;
                    Db::commit();
                } catch (\Exception $e){
                    Db::rollback();
                }
                break;
            case 'edit':
                Db::startTrans();
                try{
                    $role = $this->roleModel->get($data['id']);
                    $role->name = $data['name'];
                    $role->status = $data['status'];
                    $role->update = time();
                    $roleRes = $role->save();
                    if (!$roleRes){
                        Db::rollback();
                        return ajaxArr(ErrorCode::$OPERATIONERROR, '修改角色信息失败');
                    }
                    foreach ($data['operation_id'] as $key=>$val){
                        $operationArr[] = $val;
                    }
                    $role->operations()->detach();
                    $roleORes = $role->operations()->saveAll($operationArr);
                    if (!$roleORes){
                        Db::rollback();
                        return ajaxArr(ErrorCode::$OPERATIONERROR, '修改角色权限失败');
                    }
                    $res = true;
                    Db::commit();
                } catch (\Exception $e){
                    Db::rollback();
                }
                break;
            case 'del':
                Db::startTrans();
                try{
                    $role = $this->roleModel->get($data['id']);
                    $roleRes = $role->delete();
                    if (!$roleRes){
                        Db::rollback();
                        return ajaxArr(ErrorCode::$OPERATIONERROR, '删除角色信息失败');
                    }
                    //删除关联表
                    $operationRes = $role->operations()->detach();
                    if (!$operationRes){
                        Db::rollback();
                        return ajaxArr(ErrorCode::$OPERATIONERROR, '删除角色操作失败');
                    }
                    $res = true;
                    Db::commit();
                } catch (\Exception $e){
                    Db::rollback();
                }
                break;
        }
        if ($res){
            return ajaxArr(ErrorCode::$SUCCESS, '操作成功');
        } else{
            return ajaxArr(ErrorCode::$OPERATIONERROR, '操作失败');
        }
    }

    /**
     * 获取所有角色
     */
    public function getAllRole(){

        $roleList = $this->roleModel->all();
        if ($roleList){
            return ajaxArr(ErrorCode::$SUCCESS, 'success', $roleList);
        } else{
            return ajaxArr(ErrorCode::$LISTNULL, '列表为空');
        }
    }

}